﻿using System.Text;

namespace TPXSDK.NET.Mission.Instruction;

internal class InstSetWpYaw : Tokenizer
{
    public InstSetWpYaw(TokenizerAdmin admin)
    {
        Admin = admin;
        KeyName = "setwpyaw";
        KeyValue = (byte)FlyCmdId.CMD_SETWPYAW;
        Usage = "setwpyaw [target yaw],[Mode]";
        Description = @"Set target yaw for land";
    }

    /// <inheritdoc />
    /// <summary>
    /// 词元分析器处理过程
    /// </summary>
    public override bool Process()
    {
        var flyCmp = Admin?.Compiler; if (null == flyCmp) return false;
        var tokens = flyCmp.GetLineTokens();

        if (tokens is not { Count: 2 } &&
            tokens is not { Count: 3 })
        {
            flyCmp.ErrorMsg = $"{KeyName} Invalid Usage";
            return false;
        }

        var cmd = new FlyCmdItem
        {
            Id = (byte)KeyValue,
            LineNum = tokens[0].Row
        };

        tokens.RemoveAt(0);// Remove Flycmd Name
        if (tokens[0].Type != TokenType.NUMBER)
        {
            flyCmp.ErrorMsg = $"{KeyName} Invalid param";
            return false;
        }

        cmd.P1 = 0;
        if (tokens is { Count: 2 })// Mode param is exist
        {
            if (tokens[1].Type != TokenType.NUMBER)
            {
                flyCmp.ErrorMsg = $"{KeyName} Invalid param";
                return false;
            }
            cmd.P1 = Convert.ToByte(tokens[1].Name); ;
        }

        cmd.P2 = 0;
        cmd.P3.FloatVal = Convert.ToSingle(tokens[0].Name);
        flyCmp.CmdList.Add(cmd);
        return true;
    }

    public override void Reverse(FlyCmdItem cmd, StringBuilder sb)
    {
        sb.AppendLine($"{KeyName} {cmd.P3.FloatVal:F2},{cmd.P1}//deg");
    }
}
